home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 …ember: Reference Library / Dev.CD Dec 94.toast / Periodicals / develop / develop Issue 20 / develop 20 code / Scripting the Finder / Zawphing / Main.cp < prev    next >
Encoding:
Text File  |  1994-10-11  |  5.0 KB  |  207 lines  |  [TEXT/MMCC]

  1. /*================================================================================
  2.     Zawphing
  3.     
  4.     ©1994 Greg Anderson
  5.     greggor@apple.com
  6.     
  7.     A program that sends events to the Finder
  8.         
  9. ================================================================================*/
  10. #include <GestaltEqu.h>
  11. #include <Palettes.h>
  12.  
  13. #include "Main.h"
  14. #include "SetTypesDialog.h"
  15.  
  16. #include "MenuHandler.h"
  17. #include "EventHandler.h"
  18.  
  19. #include "MacUtilities.h"
  20. #include "DialogUtilities.h"
  21. #include "AppleEventUtilities.h"
  22. #include "Exceptions.h"
  23.  
  24. //
  25. // Prototypes for private functions:
  26. //
  27. void                    InitAll(void);
  28. pascal OSErr            QuitApplicationEvent(TAEvent& ae, TAEvent& reply, long refCon);
  29.  
  30. //
  31. // Globals defined in this file:
  32. //
  33. Rect                    gUniverseRect;
  34. RgnHandle                gUniverseRgn = nil;
  35. RgnHandle                gScratchRgn = nil;
  36. GrafPtr                    gWindowMgrPort = nil;
  37.  
  38. SysEnvRec                gThisMacintosh;
  39. Boolean                    gHasAppleEvents;
  40. Boolean                    gApplicationShouldQuit = false;
  41.  
  42. #if USESROUTINEDESCRIPTORS
  43.     static RoutineDescriptor gQuitApplicationHandlerRD    = BUILD_ROUTINE_DESCRIPTOR(uppAEEventHandlerProcInfo, QuitApplicationEvent);
  44. #endif
  45.  
  46. //----------------------------------------------------------------------------------------
  47. // main: 
  48. //----------------------------------------------------------------------------------------
  49. void main()
  50. {
  51.     RgnHandle        mouseRegion;
  52.     AppFile            theFile;
  53.     DialogPtr        splash;
  54.     short            preLoadFiles;
  55.     short            message;
  56.     short            i;
  57.     OSErr            err = noErr;
  58.     
  59.     //
  60.     // Initialize all of the ToolBox managers, change the cursor
  61.     // shape to a watch and display the splash screen.
  62.     //
  63.     InitAll();
  64.     ChangeCursor( watchCursor );
  65.     SetupMenuBar(1001, 1003);
  66.     
  67.     /*
  68.     // Schlep together our command table
  69.     //
  70.     // (command ID, menu ID, item #)
  71.     */
  72.     AddItemIDtoTable( 1, 2, 3 );
  73.     AddItemIDtoTable( 2, 2, 1 );
  74.     AddItemIDtoTable( 501, 3, 1 );
  75.     AddItemIDtoTable( 502, 3, 2 );
  76.     AddItemIDtoTable( 503, 3, 3 );
  77.     
  78.     //
  79.     // Every high-level-event-aware application needs a
  80.     // quit-application handler; otherwise, it won't quit
  81.     // when the machine is shut down (for example)
  82.     //
  83. #if USESROUTINEDESCRIPTORS
  84.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, &gQuitApplicationHandlerRD, 0, false);
  85. #else
  86.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (AEEventHandlerProcPtr) &QuitApplicationEvent, 0, false);
  87. #endif
  88.     
  89.     //
  90.     // Set the cursor back to an arrow
  91.     //
  92.     InitCursor();
  93.     
  94.     //
  95.     // Set up the initial mouse region
  96.     //
  97.     mouseRegion = NewRgn();
  98.     
  99.     //
  100.     // Set up a failure handler for failures that are not
  101.     // trapped elsewhere
  102.     //
  103.     Try
  104.     {
  105.         //
  106.         // When the program begins, open our dialog box.
  107.         // If the dialog is ever closed, we will quit (DA model)
  108.         //
  109.         OpenSetTypesDialog();
  110.         
  111.         //
  112.         // Live here until the 'gApplicationShouldQuit' flag
  113.         // becomes true
  114.         //
  115.         while(gApplicationShouldQuit == false)
  116.             HandleEvents(mouseRegion);
  117.     }
  118.     Catch(err)
  119.     {
  120.         //
  121.         // We don't expect to ever get here...
  122.         //
  123.     }
  124. } // main 
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // InitAll: 
  128. // 
  129. // Initialize various Macintosh managers
  130. //----------------------------------------------------------------------------------------
  131. void InitAll()
  132. {
  133.     OSErr    theErr;
  134.     long    heapSpace;
  135.     Ptr        appLimit;
  136.     THz        appBase;
  137.     long    gestaltResult;
  138.     short    callsToMoreMasters = 10;
  139.     
  140.     appBase = ApplicZone();
  141.     appLimit = GetApplLimit();
  142.     heapSpace = FreeMem();
  143.     MaxApplZone();
  144.     appLimit = GetApplLimit();
  145.     heapSpace = FreeMem();
  146.     while( callsToMoreMasters-- )
  147.         MoreMasters();
  148.     
  149.     InitGraf(&qd.thePort);
  150.     InitFonts();
  151.     FlushEvents(everyEvent, 0);
  152.     InitWindows();
  153.     InitMenus();
  154.     TEInit();
  155.     InitDialogs(0L);
  156.     
  157.     //
  158.     // Get the SysEnvirons record
  159.     //
  160.     SysEnvirons( 1, &gThisMacintosh );
  161.  
  162.     //
  163.     // Check to see if AppleEvents are available
  164.     //
  165.     theErr = Gestalt( gestaltAppleEventsAttr, &gestaltResult );
  166.     gHasAppleEvents = ( (theErr == noErr) && ((gestaltResult & (1L << gestaltAppleEventsPresent)) != 0) );
  167.     
  168.     //
  169.     // Set a global rectangle to hold the extent of QuickDraw workspace
  170.     // (Note:  QuickDraw coordinates range from +-32767, but there
  171.     //  are bugs in QuickDraw that make it inadvisable to go beyond
  172.     //  +-16000 or so.)
  173.     //
  174.     SetRect(&gUniverseRect,-16000,-16000,16000,16000);
  175.     gUniverseRgn = NewRgn();
  176.     RectRgn(gUniverseRgn, &gUniverseRect);
  177.     gScratchRgn = NewRgn();
  178.     GetPort(&gWindowMgrPort);
  179. } // InitAll 
  180.  
  181. //----------------------------------------------------------------------------------------
  182. // ExitProgram: 
  183. //----------------------------------------------------------------------------------------
  184. OSErr ExitProgram(CWindowPtr window, short item)
  185. {
  186.     //
  187.     // A "real" program would try to close all of its windows
  188.     // first (give the user a chance to cancel)
  189.     //
  190.     gApplicationShouldQuit = true;
  191.     
  192.     return noErr;
  193. } // ExitProgram 
  194.  
  195. //----------------------------------------------------------------------------------------
  196. // QuitApplicationEvent: 
  197. //----------------------------------------------------------------------------------------
  198. pascal OSErr QuitApplicationEvent(TAEvent& ae, TAEvent& reply, long refCon)
  199. {
  200.     //
  201.     // You should never call ExitToShell from an AppleEvent handler
  202.     //
  203.     gApplicationShouldQuit = true;
  204.     
  205.     return noErr;
  206. }
  207.